我从本地服务器下载音频文件,并保存本地路径然后播放我有问题的音频文件,我附加编码和logcat请任何人解决我的问题,
公共类MainActivity扩展了Activity {
private static String fileName = "e1.mp3";
String PATH="/data/data/com.ismail/sounds";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// URL url = new URL("http://commonsware.com/misc/test2.3gp");
URL url = new URL("http://192.168.0.2/supplications/A.1.1.1.mp3");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
// String PATH = Environment.getExternalStorageDirectory()+ "/download/";
Log.v("log_tag", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
Log.v("log_tag", "Check: ");
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try {
audioPlayer(PATH,fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void audioPlayer(String path, String fileName) throws FileNotFoundException{
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
FileInputStream fileInputStream = new FileInputStream(PATH);
try {
mp.setDataSource(fileInputStream.getFD());
// mp.setDataSource(path+"/"+filename.mp3);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
}
05-28 20:48:08.604: V/log_tag(6165): PATH: /data/data/com.ismail/sounds
05-28 20:48:18.893: V/log_tag(6165): Check:
05-28 20:49:06.513: E/MediaPlayer(6165): Unable to to create media player
05-28 20:49:21.778: W/System.err(6165): java.io.IOException: setDataSourceFD failed.: status=0x80000000
05-28 20:49:21.813: W/System.err(6165): at android.media.MediaPlayer.setDataSource(Native Method)
05-28 20:49:21.834: W/System.err(6165): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:749)
05-28 20:49:21.853: W/System.err(6165): at com.ismail.MainActivity.audioPlayer(MainActivity.java:84)
05-28 20:49:21.874: W/System.err(6165): at com.ismail.MainActivity$1.onClick(MainActivity.java:67)
05-28 20:49:21.903: W/System.err(6165): at android.view.View.performClick(View.java:2408)
05-28 20:49:21.936: W/System.err(6165): at android.view.View$PerformClick.run(View.java:8816)
05-28 20:49:21.964: W/System.err(6165): at android.os.Handler.handleCallback(Handler.java:587)
05-28 20:49:21.983: W/System.err(6165): at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 20:49:22.004: W/System.err(6165): at android.os.Looper.loop(Looper.java:123)
05-28 20:49:22.023: W/System.err(6165): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-28 20:49:22.055: W/System.err(6165): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 20:49:22.084: W/System.err(6165): at java.lang.reflect.Method.invoke(Method.java:521)
05-28 20:49:22.103: W/System.err(6165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-28 20:49:22.134: W/System.err(6165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-28 20:49:22.143: W/System.err(6165): at dalvik.system.NativeStart.main(Native Method)
05-28 20:49:23.664: E/MediaPlayer(6165): prepareAsync called in state 1
05-28 20:49:55.144: W/System.err(6165): java.lang.IllegalStateException
05-28 20:49:55.164: W/System.err(6165): at android.media.MediaPlayer.prepare(Native Method)
05-28 20:49:55.173: W/System.err(6165): at com.ismail.MainActivity.audioPlayer(MainActivity.java:97)
05-28 20:49:55.194: W/System.err(6165): at com.ismail.MainActivity$1.onClick(MainActivity.java:67)
05-28 20:49:55.223: W/System.err(6165): at android.view.View.performClick(View.java:2408)
05-28 20:49:55.244: W/System.err(6165): at android.view.View$PerformClick.run(View.java:8816)
05-28 20:49:55.274: W/System.err(6165): at android.os.Handler.handleCallback(Handler.java:587)
05-28 20:49:55.303: W/System.err(6165): at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 20:49:55.334: W/System.err(6165): at android.os.Looper.loop(Looper.java:123)
05-28 20:49:55.354: W/System.err(6165): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-28 20:49:55.374: W/System.err(6165): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 20:49:55.394: W/System.err(6165): at java.lang.reflect.Method.invoke(Method.java:521)
05-28 20:49:55.423: W/System.err(6165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-28 20:49:55.474: W/System.err(6165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-28 20:49:55.495: W/System.err(6165): at dalvik.system.NativeStart.main(Native Method)
05-28 20:49:59.664: E/MediaPlayer(6165): start called in state 1
05-28 20:50:09.786: D/dalvikvm(6165): Debugger has detached; object registry had 456 entries
答案 0 :(得分:0)
修改您的代码,然后尝试:
@Override
public void onClick(View v)
{
try {
Uri uri = Uri.fromFile(outputFile);
audioPlayer(PATH,uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void audioPlayer(String path, Uri uri) throws FileNotFoundException{
//set up MediaPlayer
MediaPlayer mp = MediaPlayer.create(this, uri);
mp.setLooping(false); // Set looping
mp.start();
}
答案 1 :(得分:0)
确保在清单中包含INTERNET权限:
<uses-permission android:name="android.permission.INTERNET"/>