这里我试图播放位于sdcard / music / audioex.m4a中的mp3文件,但它没有播放任何声音。这是我的代码,我也得到一个警告说" MediaPlayer最终确定而没有被发布"
package com.example.me.audioplay;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer mp = new MediaPlayer();
try{
Toast.makeText(this,Environment.DIRECTORY_MUSIC.toString(),Toast.LENGTH_LONG).show();
mp.setDataSource("Music/audioex.m4a");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
Toast.makeText(getApplicationContext(),"Playing an mp3 from internal",Toast.LENGTH_LONG).show();
}catch (Exception e)
{e.printStackTrace();
}
}
}
答案 0 :(得分:0)
数据源确保其绝对文件路径。数据源下面是错误的。
mp.setDataSource("Music/audioex.m4a");
我认为您需要如下所示。
File musicDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator++"music/audioex.m4a");