问题描述:

For an android app I try to write midifiles in a tempfile and play it with MediaPlayer.

Tried and followed a lot of examples but I can't get it work.

Problem is that the MediaPlayer keeps giving null as a result while creating.

Could someone look at this code and tell me what I'm doing wrong.

Thanks in advance.

 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();

}

};

}

相关阅读:
Top