Skip to content

Commit

Permalink
Merge pull request #716 from helloSingleDog/master
Browse files Browse the repository at this point in the history
#91 #95 #9 lab5 lab9 MusicPlayer
  • Loading branch information
zengsn committed May 12, 2016
2 parents 3976aa6 + 317b886 commit 61b56b0
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.hzuapps.androidworks.homeworks.net1314080903219;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.widget.EditText;
import android.widget.Toast;

import static android.support.v4.app.ActivityCompat.startActivityForResult;

public class Net1314080903219CreateMusicList extends Service {
private static final int FILE_SELECT_CODE =5 ;
// private String listName;
Net1314080903219FileStoreReader f;
// final EditText inputServer = new EditText(this);
public Net1314080903219CreateMusicList() {



}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId){

f=new Net1314080903219FileStoreReader(intent.getStringExtra("listName"));
String[] path=intent.getStringExtra("songPath").split("/");
int l=path.length;
f.store(path[l-1]+":"+intent.getStringExtra("songPath"));
f.close();
return START_STICKY;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package edu.hzuapps.androidworks.homeworks.net1314080903219;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* Created by j1 on 2016/5/6.
*/
public class Net1314080903219FileStoreReader {
private File object=null;
private FileInputStream fin=null;
private FileOutputStream fout=null;
private int fileLength=0;
public Net1314080903219FileStoreReader(String name){
object=new File("/storage/sdcard1/Music/"+name+".txt");
System.out.println(object.getPath());
if(object.exists()){
try {
fin=new FileInputStream(object);
} catch (FileNotFoundException e) {
e.printStackTrace();
return ;
}
}

try {
fout=new FileOutputStream(object);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public boolean close(){
try {
if (fin!=null) {
fin.close();
}
if(fout!=null){
fin.close();
}

} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

public int store(String data){
if(fout==null){
return -1;
}
byte[] by=data.getBytes();
try {
fout.write(by);
fout.flush();
} catch (IOException e) {
e.printStackTrace();
return -1;
}

return by.length;
}

public int read(byte[] data){
if(fin==null){
return -1;
}
int length;
try {
length=fin.available();
} catch (IOException e) {
e.printStackTrace();
return -1;
}
if(length==0){
return 0;
}
try {
fin.read(data);
} catch (IOException e) {
e.printStackTrace();
}
return data.length>length?length:data.length;
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
package edu.hzuapps.androidworks.homeworks.net1314080903219;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class Net1314080903219MainActivity extends AppCompatActivity {
import android.widget.EditText;
import android.widget.Toast;



public class Net1314080903219MainActivity extends AppCompatActivity {
private String listName;
private final int LIST_CONTENT_CODE=100;
private final int FILE_SELECT_CODE=1000;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1314080903219_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
FloatingActionButton email = (FloatingActionButton) findViewById(R.id.email);
email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "contact us", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

FloatingActionButton info = (FloatingActionButton) findViewById(R.id.info);
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
Snackbar.make(view, "this is our displayer", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

}

@Override
Expand All @@ -51,13 +74,77 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public void onClick(View view){
Button bu=(Button) findViewById(R.id.button);
/* Button bu=(Button) findViewById(R.id.button);
if(bu.getText().equals("OPEN THE DOOR")){
bu.setText("SEE YOUR SISTER");
}
else {
bu.setText("OPEN THE DOOR");
}
}*/

final EditText inputServer = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("List name").setIcon(android.R.drawable.ic_dialog_info).setView(inputServer)
.setNegativeButton("Cancel", null);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listName = inputServer.getText().toString();

Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.setType("*/mp3");
intent1.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent1, LIST_CONTENT_CODE);

}
});
builder.show();


// startActivityForResult(intent,LIST_CONTENT_CODE);
// startService(intent);

}

public void play(View view){
showFileChooser();

}
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/mp3");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(intent, FILE_SELECT_CODE );
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
intent = new Intent();
intent.setClass(this, Net1314080903219MediaPlayerService.class);
intent.putExtra("song", uri.getPath());
// System.out.println(intent.getStringExtra("song"));
startService(intent);
}
break;
case LIST_CONTENT_CODE:
if (resultCode == RESULT_OK) {{
Uri uri = data.getData();
intent = new Intent();
intent.setClass(this, Net1314080903219CreateMusicList.class);
intent.putExtra("songPath",uri.getPath());
intent.putExtra("listName",listName);
startService(intent);
}}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package edu.hzuapps.androidworks.homeworks.net1314080903219;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

import java.io.IOException;

import static android.support.v4.app.ActivityCompat.startActivityForResult;

public class Net1314080903219MediaPlayerService extends Service {
MediaPlayer player = new MediaPlayer();

public Net1314080903219MediaPlayerService() {

/* try {
player.reset();
player.setDataSource(path);
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}*/
}


@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

//在这里我们需要实例化MediaPlayer对象
public void onCreate(){

super.onCreate();
//我们从raw文件夹中获取一个应用自带的mp3文件

System.out.println("sfsfsfsf dsf fdfd fsdf sf ffsfs");


}

/**
* 该方法在SDK2.0才开始有的,替代原来的onStart方法
*/
public int onStartCommand(Intent intent, int flags, int startId){
if(!player.isPlaying()){
// System.out.println(String.valueOf(intent.getCharSequenceArrayExtra("song")));
try {
player.reset();
player.setDataSource(intent.getStringExtra("song"));
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
return START_STICKY;
}

public void onDestroy(){
//super.onDestroy();
if(player.isPlaying()){
player.stop();
}
player.release();
}




//后退播放进度
public void haveFun(){
if(player.isPlaying() && player.getCurrentPosition()>2500){
player.seekTo(player.getCurrentPosition()-2500);
}
}
}

11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_net1314080903219_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/Net1314080903219second"
android:background="@drawable/net1314080903219second"
tools:context="edu.hzuapps.androidworks.homeworks.net1314080903219.Net1314080903219MainActivity">

<android.support.design.widget.AppBarLayout
Expand All @@ -25,11 +25,18 @@
<include layout="@layout/content_net1314080903219_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_info" />
</android.support.design.widget.CoordinatorLayout>
Loading

0 comments on commit 61b56b0

Please sign in to comment.