我的应用程序有一个按钮,允许用户从手机中选择铃声。代码如下:
public void Btn_Ringtone_Click(View v){
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if((requestCode == 1) && (resultCode == RESULT_OK)){
ringtoneUri = data.getData();
ringtoneLabel.setText(ringtoneUri.toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
这似乎有效。
后来我想和媒体播放器一起使用相同的Uri来播放所选择的铃声。我的代码如下:
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.i("My tag...", "Service started");
String uriAsString = intent.getStringExtra("ASM_URI");
Log.i("My tag...", uriAsString);
//Uri uri = Uri.parse(uriAsString);
//Uri uri = Uri.parse(uriAsString.replace("file:/", "file:///"));
Uri uri = getContentUriForPath(uriAsString);
MediaPlayer mp = new MediaPlayer();
mp.create(this, uri);
//mp = MediaPlayer.create(this, uri);
//mp = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
/*
try {
mp.setDataSource(getApplicationContext(), uri);
}
catch (IOException e) {
e.printStackTrace();
}
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri);
ringtone.play();
mp.start();
this.stopSelf();
*/
//return super.onStartCommand(intent, flags, startId);
return START_NOT_STICKY;
}
我留下了评论代码,因为这些是我尝试过的东西 - 到目前为止没有任何工作。
我认为问题与权限,文件位置或URI本身有关。但我无法解决它。
至于我添加的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
到清单文件。
根据我的日志,uri看起来像这样:
内容://媒体/外部/音频/媒体/ 3250
这看起来不对......在文件被挑选后,它在ringtoneLabel中看起来并不像那样。
问题:如何从本地文件中正确选择,存储和播放URI?
答案 0 :(得分:0)
我认为这会对你有帮助......
public class MainActivity extends AppCompatActivity {
private Button btnPick, btnPlay, btnPause, btnStop;
private TextView txtSource;
final static int requestPick = 3;
private Uri fileUri;
final static int RQS_PERMISSION_READ_EXTERNAL_STORAGE = 4;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPick = (Button) findViewById(R.id.btnPick);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPause = (Button) findViewById(R.id.btnPause);
btnStop = (Button) findViewById(R.id.btnStop);
txtSource = (TextView) findViewById(R.id.srcFile);
//Pick audio file
btnPick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(
Intent.createChooser(intent, "ACTION_PICK"),
requestPick);
}
});
//Play the selected audio
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fileUri == null) {
Toast.makeText(MainActivity.this,
"No file selected",
Toast.LENGTH_LONG).show();
} else {
playerPrepare();
playerStart();
}
}
});
//Pause the player
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playerPause();
}
});
//Stop the player
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playerStop();
}
});
}
private void playerStop() {
player.stop();
}
private void playerPause() {
player.pause();
}
private void playerStart() {
player.start();
}
private void playerPrepare() {
try {
player.prepare();
} catch (IllegalStateException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == requestPick) {
fileUri = data.getData();
txtSource.setText(fileUri.toString());
//Check and request Permission at runtime
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
RQS_PERMISSION_READ_EXTERNAL_STORAGE);
return;
}
resetPlayer();
setPlayerSource(fileUri);
}
}
}
private void setPlayerSource(Uri fileUri) {
try {
player.setDataSource(MainActivity.this, fileUri);
} catch (IllegalArgumentException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IllegalStateException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private void resetPlayer() {
if (player == null) {
player = new MediaPlayer();
}
player.reset();
}
}
这个的xml是....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="mjuyelrana.com.audioselect.MainActivity">
<Button
android:id="@+id/btnPick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pick"
android:textAllCaps="false" />
<TextView
android:id="@+id/srcFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/source" />
<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/play"
android:textAllCaps="false" />
<Button
android:id="@+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pause"
android:textAllCaps="false" />
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:textAllCaps="false" />
不要忘记添加权限......
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>