对于Android应用程序,我尝试在tempfile中编写midifiles并使用 MediaPlayer 进行播放。 尝试并遵循了很多例子,但我无法让它发挥作用。 问题是 MediaPlayer 在创建时不断提供null。 有人可以看看这段代码并告诉我我做错了什么。 提前谢谢。
package com.example.miditest;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class MainActivity extends Activity {
public void init(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Playbutton1 = (Button)findViewById(R.id.button1);
Playbutton1.setOnClickListener(
new Button.OnClickListener(){
@Override
public void onClick(View v){
Button Playbutton1 = (Button)findViewById(R.id.button1);
Playbutton1.setText("test1");
WriteMidiFile();
if(tempMid.exists()){
Toast.makeText(getApplication(), "File bestaat", Toast.LENGTH_LONG);
};
Playfile();
}
}
);
}
MediaPlayer mediaPlayer;
/* This one with a stored file in raw folder worked!
* the file stored was generated in java with the same code to be sure there's no
* difference.
public void Playfile(){
mediaPlayer = MediaPlayer.create(this,R.raw.testmid);
mediaPlayer.start();
mediaPlayer.release();
}
*/
public void Playfile(){
// test that the tempfile can be found.
if(tempMid.exists()){
Toast.makeText(getApplication(), "File bestaat", Toast.LENGTH_LONG);
};
// creating mediaPlayer gives NULL
Uri Uristring = Uri.fromFile(tempMid);
mediaPlayer = MediaPlayer.create(getBaseContext(), Uristring);
mediaPlayer.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
File tempMid;
public void WriteMidiFile(){
/* Writing midi data to tempfile
* Tested positive to be a valid and playable midi file
*/
try {
tempMid = File.createTempFile("Chord",".mid",this.getCacheDir());
DataOutputStream data;
data = new DataOutputStream(new FileOutputStream(tempMid.getAbsolutePath()));
data.write((byte)'M');
data.write((byte)'T');
data.write((byte)'h');
data.write((byte)'d');
data.writeInt(6);
data.writeInt(1);
data.writeShort((short)480);
data.write((byte)'M');
data.write((byte)'T');
data.write((byte)'r');
data.write((byte)'k');
data.writeInt(50); //tracklengte
data.write(0x00);
data.writeShort((short)49176);//channel gitaar
data.writeShort((short)144);//tijd voor note on
data.writeShort((short)16464);//40 50
data.write(0x00);
data.writeShort((short)15184);//3b 50
data.write(0x00);
data.writeShort((short)14160);//37 50
data.write(0x00);
data.writeShort((short)12880);//32 50
data.write(0x00);
data.writeShort((short)11600);//2d 50
data.write(0x00);
data.writeShort((short)10320);//28 50
data.writeShort((short)36471);//tijd voor note off
data.writeShort((short)16384);//64 00
data.write(0x00);
data.writeShort((short)15104);//59 00
data.write(0x00);
data.writeShort((short)14080);//55 00
data.write(0x00);
data.writeShort((short)12800);//50 00
data.write(0x00);
data.writeShort((short)11520);//45 00
data.write(0x00);
data.writeShort((short)10240);//40 00
data.writeInt(16723712);
data.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}