Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;

import android.media.MediaPlayer;
import android.util.Log;
import android.webkit.MimeTypeMap;

public class Main {
    /**
     * To add hack to .3gp file
     * @param url
     * @return
     */
    public static String getAudioMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        Log.w("KKIM", " Sending File Extension = " + extension);
        if (extension != null) {
            if (extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("3gpp")) {
                if (is3gpFileAudio(url)) {
                    type = "audio/3gpp";
                } else {
                    MimeTypeMap mime = MimeTypeMap.getSingleton();
                    type = mime.getMimeTypeFromExtension(extension);
                }
            } else {
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                type = mime.getMimeTypeFromExtension(extension);
            }

            if (type.equalsIgnoreCase("application/ogg")) {
                type = "audio/ogg";
                Log.d("KKIM", "Formatting Audio File Type from application/ogg to audio/ogg");
            }
        }
        return type;
    }

    public static boolean is3gpFileAudio(String url) {
        int height = 0;
        File mediaFile = new File(url);
        try {
            MediaPlayer mp = new MediaPlayer();
            FileInputStream fs;
            FileDescriptor fd;
            fs = new FileInputStream(mediaFile);
            fd = fs.getFD();
            mp.setDataSource(fd);
            mp.prepare();
            height = mp.getVideoHeight();
            mp.release();
        } catch (Exception e) {
            Log.e("KKIM", "Exception trying to determine if 3gp file is video.", e);
        }
        Log.i("KKIM", "The height of the file is " + height);
        return height == 0;
    }
}